﻿using DelonixRegia.Classes;
using DelonixRegia.Classes.MiniBarItems;
using DelonixRegia.Classes.Rooms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class RoomCheckout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxRoomNo.Text))
            {
                Classes.RoomBooking rb = RoomBookingDBManager.getBookingByRoomNo(Convert.ToInt32(tbxRoomNo.Text));

                if (rb != null)
                {
                    lblRoomNo.Text = rb.roomNo.ToString();
                    lblRoomType.Text = RoomDBManager.getRoomType(RoomDBManager.getRoomByRoomNo(rb.roomNo).roomType).name;
                    lblHiddenBookingID.Text = rb.bookingID.ToString();

                    lblCheckIn.Text = rb.checkIn.ToString();
                    lblCheckOut.Text = rb.checkOut.ToString();

                    lblFirstName.Text = rb.firstName;
                    lblLastName.Text = rb.lastName;
                    lblContact.Text = rb.contactNo;
                    lblEmail.Text = rb.emailAddress;

                    pnlFirstStep.Visible = false;
                    pnlSecondStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                }
                else
                    ClientScriptMgr.showErrorBox("The room number you specified is not found or not occupied!");
            }
            else if (!string.IsNullOrEmpty(tbxGuestNRIC.Text))
            {
                Classes.RoomBooking rb = RoomBookingDBManager.getBookingByNRIC(tbxGuestNRIC.Text);

                if (rb != null)
                {
                    lblRoomNo.Text = rb.roomNo.ToString();
                    lblRoomType.Text = RoomDBManager.getRoomType(RoomDBManager.getRoomByRoomNo(rb.roomNo).roomType).name;

                    lblCheckIn.Text = rb.checkIn.ToString();
                    lblCheckOut.Text = rb.checkOut.ToString();

                    lblFirstName.Text = rb.firstName;
                    lblLastName.Text = rb.lastName;
                    lblContact.Text = rb.contactNo;
                    lblEmail.Text = rb.emailAddress;

                    pnlFirstStep.Visible = false;
                    pnlSecondStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                }
                else
                    ClientScriptMgr.showErrorBox("The NRIC Number you specified is not found!");
            }
            else
                ClientScriptMgr.showErrorBox("You need to enter either the <i>Room Number</i> or <i>Guest\'s NRIC</i>!");
        }

        protected void btnProceed_Click(object sender, EventArgs e)
        {
            Classes.RoomBooking rb = RoomBookingDBManager.getBookingByID(Convert.ToInt32(lblHiddenBookingID.Text));

            if (rb != null)
            {
                lblInvFirstName.Text = rb.firstName;
                lblInvLastName.Text = rb.lastName;
                lblInvNRIC.Text = rb.nric;
                lblInvAddress.Text = rb.address;
                lblInvContact.Text = rb.contactNo;
                lblInvEmail.Text = rb.emailAddress;

                lblInvNoOfAdults.Text = rb.noOfAdults.ToString();
                lblInvNoOfChildren.Text = rb.noOfChildren.ToString();
                lblInvCheckIn.Text = rb.checkIn.ToString();
                lblInvCheckOut.Text = DateTime.Now.ToString();

                lblInvRoomNo.Text = rb.roomNo.ToString();
                lblInvRoomType.Text = RoomDBManager.getRoomType(RoomDBManager.getRoomByRoomNo(rb.roomNo).roomType).name;

                lblInvTotalGuest.Text = (rb.noOfAdults + rb.noOfChildren).ToString();
                lblInvTotalDays.Text = Math.Round((DateTime.Now - rb.checkIn).TotalDays, 2).ToString();
                lblInvRoomRate.Text = RoomDBManager.getRoomByRoomNo(rb.roomNo).roomRate.ToString();
                lblInvTotalStayPrice.Text = Math.Round(Convert.ToInt32(lblInvTotalGuest.Text) * Convert.ToDecimal(lblInvTotalDays.Text) * Convert.ToDecimal(lblInvRoomRate.Text), 2).ToString();

                lblInvMiniBarPrice.Text = "0";
                lblInvGrossTotal.Text = lblInvTotalStayPrice.Text;
                lblInvGST.Text = (Convert.ToDouble(lblInvGrossTotal.Text) * 0.07).ToString();
                lblInvTotalPayable.Text = Math.Round(Convert.ToDecimal(lblInvGrossTotal.Text) + Convert.ToDecimal(lblInvGST.Text), 2).ToString();

                pnlSecondStep.Visible = false;
                pnlThirdStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToThirdStep", "transitionToThirdStep();", true);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (lbMiniBar.SelectedItem != null)
            {
                lbInvMiniBar.Items.Add(new ListItem(lbMiniBar.SelectedItem.Text, lbMiniBar.SelectedItem.Value));
                lblInvMiniBarPrice.Text = (Convert.ToDecimal(lblInvMiniBarPrice.Text) + MiniBarDBManager.getMiniBarItemByID(Convert.ToInt32(lbMiniBar.SelectedItem.Value)).cost).ToString();
                lblInvGrossTotal.Text = Math.Round(Convert.ToDecimal(lblInvTotalStayPrice.Text) + Convert.ToDecimal(lblInvMiniBarPrice.Text), 2).ToString();
                lblInvGST.Text = Math.Round(Convert.ToDouble(lblInvGrossTotal.Text) * 0.07, 2).ToString();
                lblInvTotalPayable.Text = Math.Round(Convert.ToDecimal(lblInvGrossTotal.Text) + Convert.ToDecimal(lblInvGST.Text), 2).ToString();
            }
        }

        protected void btnCompleteCheckout_Click(object sender, EventArgs e)
        {
            Invoice i = new Invoice();
            i.bookingID = Convert.ToInt32(lblHiddenBookingID.Text);
            i.noOfDaysStay = (int)Math.Ceiling(Convert.ToDecimal(lblInvTotalDays.Text));
            i.totalMinibarCost = Convert.ToDecimal(lblInvMiniBarPrice.Text);
            i.totalStayCost = Convert.ToDecimal(lblInvTotalStayPrice.Text);
            i.totalGST = Convert.ToDecimal(lblInvGST.Text);
            i.netPayable = Convert.ToDecimal(lblInvTotalPayable.Text);

            if (RoomCheckoutDBManager.Checkout(i))
            {
                pnlThirdStep.Visible = false;
                pnlFourthStep.Visible = true;
                Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFourthStep", "transitionToFourthStep();", true);
            }
            else
                ClientScriptMgr.Execute("$('#checkoutErrorBox').html('<strong>Error:</strong> Unable to process checkout request, pls try again later.');$('#checkoutErrorBox').hide().show();");
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}